package com.lw;

import com.lw.constant.GameState;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 窗口
 */
public class GameWin extends JFrame {

    /**
     * 游戏状态 ： 0未开始，1游戏中，2通关失败，3通关成功，4暂停
     */
    static GameState state = GameState.NOT_STARTED;

    //窗口宽高
    public static final int WIDTH = 1440;
    public static final int HEIGHT = 900;

    Image offScreenImage;//解决屏幕闪烁问题

    Bg bg = new Bg();//背景绘制
    Enemy enemy;
    Enemy boss;
    boolean isBoss = false;//是否生成boss
    MyFish myFish = new MyFish();

    int time = 0;//计数器
    double random;

    void logic(){
        //关卡难度
        if(GameUtils.score<5){
            GameUtils.level = 0;
            myFish.level = 1;
        }else if(GameUtils.score<=15){
            GameUtils.level = 1;
        }else if(GameUtils.score<=50){
            GameUtils.level = 2;
            myFish.level = 2;
        }else if(GameUtils.score<=150){
            GameUtils.level = 3;
            myFish.level = 3;
        }else if(GameUtils.score<=300){
            GameUtils.level = 4;
        }else{
            state = GameState.SUCCESS;
        }

        random = Math.random();
        switch (GameUtils.level){//根据难度等级生成不同鱼
            case 4:
                if(time%60==0){
                    //创建鱼后加入集合
                    if(random>0.2){
                        boss = new Enemy_Boss();//生成boss
                        isBoss = true;
                    }
                }
            case 3:
            case 2:
                if(time%30==0){//此处是保证生成鱼不会生成过快
                    //创建鱼后加入集合
                    if(random>0.5){
                        enemy = new Enemy_3_L();
                    }else{
                        enemy = new Enemy_3_R();
                    }
                    GameUtils.enemyList.add(enemy);
                }
            case 1:
                if(time%20==0){//此处是保证生成鱼不会生成过快
                    //创建鱼后加入集合
                    if(random>0.5){
                        enemy = new Enemy_2_L();
                    }else{
                        enemy = new Enemy_2_R();
                    }
                    GameUtils.enemyList.add(enemy);
                }
            case 0:
                if(time%10==0){//此处是保证生成鱼不会生成过快
                    //创建鱼后加入集合
                    if(random>0.5){
                        enemy = new Enemy_1_L();
                    }else{
                        enemy = new Enemy_1_R();
                    }
                    GameUtils.enemyList.add(enemy);
                }
                break;
        }
        for (Enemy enemy1 : GameUtils.enemyList) {
            enemy1.x = enemy1.x+enemy1.dir*enemy1.speed;

            //boss鱼碰到其他鱼，其他鱼消失
            if(isBoss){
                if(boss.getRec().intersects(enemy1.getRec())){
                    enemy1.x = -200;
                    enemy1.y = -200;
                }
            }

            //主角和敌人碰撞检测
            if(myFish.getRec().intersects(enemy1.getRec())){
                //主角等级比敌人高才能干掉敌人
                if(myFish.level>=enemy1.type){
                    enemy1.x = -200;
                    enemy1.y = -200;
                    GameUtils.score+= enemy1.score;
                }else{//否则游戏失败
                    state = GameState.FAILED;
                }
            }
        }

        //boss鱼碰到主角，则游戏失败
        if(isBoss&&boss.getRec().intersects(myFish.getRec())){
            state = GameState.FAILED;
        }

    }

    @Override
    public void paint(Graphics g) {
        if(offScreenImage==null) offScreenImage = createImage(WIDTH,HEIGHT);
        Graphics imgG = offScreenImage.getGraphics();

        //绘制游戏中背景
        bg.paintSelf(imgG,myFish.level);

        switch (state) {
            case NOT_STARTED -> {
            }
            case IN_PROGRESS -> {
                //绘制主角
                myFish.paintSelf(imgG);

                //游戏逻辑
                logic();

                //遍历绘制鱼
                for (Enemy enemy : GameUtils.enemyList) {
                    enemy.paintSelf(imgG);
                }
                if(isBoss){
                    boss.x = boss.x+boss.speed* boss.dir;
                    boss.paintSelf(imgG);

                    if(boss.x<0){//boss出现提前预警
                      imgG.setColor(Color.red);
                      imgG.fillRect(boss.x,boss.y,WIDTH+100,boss.height/30);
                    }

                }
            }
            case FAILED -> {
                //遍历绘制鱼
                for (Enemy enemy : GameUtils.enemyList) {
                    enemy.paintSelf(imgG);
                }
                if(isBoss){
                    boss.paintSelf(imgG);
                }
            }
            case SUCCESS -> {
                myFish.paintSelf(imgG);
            }
            case PAUSED -> {
            }
        }

        g.drawImage(offScreenImage,0,0,null);
    }

    /**
     * 加载
     */
    public void launch() {
        this.setVisible(true);//窗口可见
        this.setSize(WIDTH, HEIGHT);
        this.setLocationRelativeTo(null);//居中显示
        this.setTitle("大鱼吃小鱼");
//        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()==1&&state==GameState.NOT_STARTED){//鼠标左键点击
                    state = GameState.IN_PROGRESS;
                    repaint();
                }else if(e.getButton()==1&&(state==GameState.FAILED||state==GameState.SUCCESS)){//重新开始游戏
                    reStartGame();
                    state = GameState.IN_PROGRESS;
                }
            }
        });

        this.addKeyListener(new KeyAdapter() {//键盘事件
            @Override
            public void keyPressed(KeyEvent e) {//按压
                int kc = e.getKeyCode();
                switch (kc){
                    case KeyEvent.VK_W -> GameUtils.up = true;
                    case KeyEvent.VK_S -> GameUtils.down = true;
                    case KeyEvent.VK_A -> GameUtils.left = true;
                    case KeyEvent.VK_D -> GameUtils.right = true;
                    case KeyEvent.VK_SPACE -> {//空格键实现暂停、开始
                        if(state==GameState.PAUSED) state = GameState.IN_PROGRESS;
                        else if(state==GameState.IN_PROGRESS) state = GameState.PAUSED;
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {//抬起
                int kc = e.getKeyCode();
                switch (kc){
                    case KeyEvent.VK_W -> GameUtils.up = false;
                    case KeyEvent.VK_S -> GameUtils.down = false;
                    case KeyEvent.VK_A -> GameUtils.left = false;
                    case KeyEvent.VK_D -> GameUtils.right = false;
                }
            }
        });

        while (true){
            repaint();
            time++;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    /**
     * 重开游戏
     */
    void reStartGame(){
        GameUtils.enemyList.clear();
        time = 0;
        myFish.level = 0;
        myFish.x = WIDTH/2;
        myFish.y = HEIGHT/2;
        myFish.width = 50;
        myFish.height = 50;
        boss = null;
        isBoss = false;
        GameUtils.score = 0;
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
